package cn.iocoder.yudao.module.system.controller.admin.productionplan;

import cn.iocoder.yudao.module.system.controller.admin.productionplandetail.vo.ProductionPlanDetailRespVO;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.error;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.system.controller.admin.productionplan.vo.*;
import cn.iocoder.yudao.module.system.dal.dataobject.productionplan.ProductionPlanDO;
import cn.iocoder.yudao.module.system.service.productionplan.ProductionPlanService;

@Tag(name = "管理后台 - 生产计划")
@RestController
@RequestMapping("/system/production-plan")
@Validated
public class ProductionPlanController {

    @Resource
    private ProductionPlanService productionPlanService;

    @PostMapping("/create")
    @Operation(summary = "创建生产计划")
    @PreAuthorize("@ss.hasPermission('system:production-plan:create')")
    public CommonResult<Integer> createProductionPlan(@Valid @RequestBody ProductionPlanSaveReqVO createReqVO) {
        return success(productionPlanService.createProductionPlan(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新生产计划")
    @PreAuthorize("@ss.hasPermission('system:production-plan:update')")
    public CommonResult<Boolean> updateProductionPlan(@Valid @RequestBody ProductionPlanSaveReqVO updateReqVO) {
        productionPlanService.updateProductionPlan(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除生产计划")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('system:production-plan:delete')")
    public CommonResult<Boolean> deleteProductionPlan(@RequestParam("id") Integer id) {
        productionPlanService.deleteProductionPlan(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除生产计划")
                @PreAuthorize("@ss.hasPermission('system:production-plan:delete')")
    public CommonResult<Boolean> deleteProductionPlanList(@RequestParam("ids") List<Integer> ids) {
        productionPlanService.deleteProductionPlanListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得生产计划")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:production-plan:query')")
    public CommonResult<ProductionPlanRespVO> getProductionPlan(@RequestParam("id") Integer id) {
        ProductionPlanDO productionPlan = productionPlanService.getProductionPlan(id);
        return success(BeanUtils.toBean(productionPlan, ProductionPlanRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得生产计划分页")
    @PreAuthorize("@ss.hasPermission('system:production-plan:query')")
    public CommonResult<PageResult<ProductionPlanRespVO>> getProductionPlanPage(@Valid ProductionPlanPageReqVO pageReqVO) {
        PageResult<ProductionPlanRespVO> pageResult = productionPlanService.getProductionPlanPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ProductionPlanRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出生产计划 Excel")
    @PreAuthorize("@ss.hasPermission('system:production-plan:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportProductionPlanExcel(@Valid ProductionPlanPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ProductionPlanRespVO> list = productionPlanService.getProductionPlanPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "生产计划.xls", "数据", ProductionPlanRespVO.class,
                        BeanUtils.toBean(list, ProductionPlanRespVO.class));
    }

    @PostMapping("/import-excel")
    @Operation(summary = "导入生产计划 Excel")
    @PreAuthorize("@ss.hasPermission('system:production-plan:import-excel')")
    @ApiAccessLog(operateType = EXPORT)
    public CommonResult<Integer> importProductionPlanExcel(@Valid ProductionPlanImportReqVO importReqVO) throws IOException {
        List<ProductionPlanImportExcelVO> list = ExcelUtils.read(importReqVO.getFile(), ProductionPlanImportExcelVO.class);
        if(list==null||list.size()==0){
            return error(10001,"导入内容不能为空");
        }
        if(list.get(0).getOpenStime()==null||list.get(0).getOpenStime().equals("")){
            return error(10002,"开线时间不能为空");
        }
        if(list.get(0).getOpenEtime()==null||list.get(0).getOpenEtime().equals("")){
            return error(10002,"开线时间不能为空");
        }
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/M/d HH:mm:ss");
        // 预处理字符串，确保小时部分是两位数字
        String processedDateTimeStr1 = list.get(0).getOpenStime().replaceAll(" (\\d):", " 0$1:");
        String processedDateTimeStr2 = list.get(0).getOpenEtime().replaceAll(" (\\d):", " 0$1:");
        ProductionPlanSaveReqVO createReqVO = new ProductionPlanSaveReqVO();
        createReqVO.setNumber(list.get(0).getNumber());
        createReqVO.setOpenStime(String.valueOf(LocalDateTime.parse(processedDateTimeStr1, formatter)));
        createReqVO.setOpenEtime(String.valueOf(LocalDateTime.parse(processedDateTimeStr2, formatter)));
        List<ProductionPlanDetailRespVO> detailRespVOS = BeanUtils.toBean(list, ProductionPlanDetailRespVO.class);
        createReqVO.setDetailRespVOS(detailRespVOS);
        return success(productionPlanService.createProductionPlan(createReqVO));
    }

}
